package com.sxw.controller;

import com.sxw.annotation.PreventDuplicateSubmit;
import com.sxw.dto.LoginDto;
import com.sxw.dto.PlayerSessionDto;
import com.sxw.user.domain.SysUser;
import com.sxw.user.service.SysUserService;
import com.sxw.utils.R;
import com.sxw.utils.RedisUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequiredArgsConstructor
public class SysUserController {

    private final SysUserService sysUserService;
    private final RedisUtils redisUtils;

    @PostMapping("/login")
    @PreventDuplicateSubmit
    public R<String> login(@RequestBody LoginDto loginDto) {
        return sysUserService.login(loginDto);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        // 从请求中获取用户ID
        String userName = (String) request.getAttribute("userName");
        if (ObjectUtils.isEmpty(userName)) {
            return R.fail("未登录");
        }
        return sysUserService.logout(userName);
    }
}